
//获取应用实例
const app = getApp()

Page({
  data: {
    baseUseMonth: null,
    userId: null,
    id: null,
    baseMoney: null,
    basePer: 0.0,
    payMoney: 0.00,
    payMoneyCheck: 0.00,
    action: null,
    wh:0
  },
  onShow: function () {
    var that = this;
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          "wh": res.windowHeight - 48 -wx.getSystemInfoSync().statusBarHeight
        })
      }
    })
  },
  goBack: function(){
    wx.navigateBack({
      delta: 1 //返回的页面数，如果 delta 大于现有页面数，则返回到首页,
    });
  },
  onLoad: function (options) {
    if (app.globalData.userId) {
      this.setData({
        userId: app.globalData.userId
      })
    }
    var action = options.action;
    if (action != 'add') {
      var id = options.id;
      var baseUseMonth = options.baseUseMonth;
      var baseMoney = options.baseMoney;
      var basePer = options.basePer;
      var payMoney = options.payMoney;
      var payMoneyCheck = options.payMoneyCheck;
      this.setData({
        id: id,
        baseUseMonth: baseUseMonth,
        baseMoney: baseMoney,
        basePer: basePer,
        payMoney: payMoney,
        payMoneyCheck: payMoneyCheck,
        action: action
      })
    }
    else {
      var date = new Date();
      var year = date.getFullYear();//年
      var month = date.getMonth() + 1;//月
      month = month < 10 ? "0" + month : month;
      var baseUseMonth = year + '年' + month + '月';
      this.setData({
        baseUseMonth: baseUseMonth,
        action: action
      })
    }
  },
  bindDateChange(e) {
    this.setData({
      baseUseMonth: e.detail.value.replace('-','年') + '月'
    })
  },
  payMoneyChange(e) {
    var basePer = 0.0;
    var baseMoney = e.detail.value;
    if (baseMoney == '') {
      basePer = 0.0;
    }
    else if (baseMoney <= 3000) {
      basePer = 0.5;
    }
    else if (baseMoney > 3000 && baseMoney <= 5000) {
      basePer = 1.0;
    }
    else if (baseMoney > 5000 && baseMoney <= 10000) {
      basePer = 1.5;
    }
    else if (baseMoney > 10000) {
      basePer = 2.0;
    }
    var payMoney = (baseMoney * basePer * 0.01).toFixed(2);
    this.setData({
      baseMoney: baseMoney,
      basePer: basePer,
      payMoney: payMoney,
      payMoneyCheck: payMoney
    })
  },
  submit(e) {
    wx.showToast({
      title: 'loading',
      icon: 'loading',
      duration: 5000
    })
    var that = this;
    wx.request({
      url: 'https://www.xinhuo.site/api/costBase/save',
      data: {
        baseUseMonth: that.data.baseUseMonth,
        userId: that.data.userId,
        id: that.data.id,
        baseMoney: that.data.baseMoney,
        basePer: that.data.basePer,
        payMoney: that.data.payMoney,
        payMoneyCheck: that.data.payMoneyCheck,
      },
      method: 'POST',
      header: {
        'Content-Type': 'application/json'
      },
      success(res) {
        wx.hideToast();
        if (res.data.syscode == 0) {
          wx.showToast({
            title: '提交成功',
            duration: 1000
          })
          wx.navigateBack({
            delta: 1
          });
        }
        else {
          wx.showToast({
            title: '操作失败',
            duration: 3000
          })
        }
      },
      fail(res) {
        wx.hideToast();
        wx.showToast({
          title: '操作失败，请检查网络',
          duration: 3000
        })
      }
    })
  }
});